package xyz.huanggy.minio.upload.configuration;

import io.minio.MinioClient;
import lombok.SneakyThrows;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xyz.huanggy.minio.upload.properties.MinioProperties;

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({MinioClient.class})
public class MinioCofig {
//    @Value("${minio.endpoint}")
//    private String url;
//    @Value("${minio.access-key}")
//    private String accessKey;
//    @Value("${minio.access-secret}")
//    private String secretKey;

//    private MinioProperties minioProperties;

//    @Bean("minioClient")
//    public MinioClient getMinioClient() {
//
//        return MinioClient.builder().endpoint(minioProperties.getEndpoint())
//                .credentials(minioProperties.getAccessKey(), minioProperties.getAccessSecret()).build();
//    }

    @Bean
    @SneakyThrows
    @ConditionalOnMissingBean(PearlMinioClient.class)
    public PearlMinioClient minioClient(OssProperties ossProperties) {
        MinioClient minioClient = MinioClient.builder()
                .endpoint(ossProperties.getEndpoint())
                .credentials(ossProperties.getAccessKey(), ossProperties.getAccessSecret())
                .build();
        return new PearlMinioClient(minioClient);
    }

    @Bean
    @ConditionalOnBean({MinioClient.class})
    @ConditionalOnMissingBean(MinioTemplate.class)
    public MinioTemplate minioTemplate(PearlMinioClient pearlMinioClient, OssProperties ossProperties) {
        return new MinioTemplate(pearlMinioClient, ossProperties);
    }


}
